//
//  MainViewController.m
//  iOS_NongLian
//
//  Created by n369 on 15/12/14.
//  Copyright © 2015年 NongLian. All rights reserved.
//

#import "TabBarViewController.h"
#import "RootNavigationController.h"
#import "ParentViewController.h"

static TabBarViewController * shareMain = nil;

@interface TabBarViewController () <UITabBarControllerDelegate>{
    NSMutableArray * _controllers;
}

@end

@implementation TabBarViewController

- (instancetype)init
{
    if (!shareMain) {
        shareMain = [super init];
    }
    return shareMain;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setTabBar];//设置没有上边缘线条
    [self createView];//创建TabbarCell
    
}

- (void)setTabBar {
    
    UIImage * tabBarBackground = [UIImage imageNamed:@"clear"]; //透明图片
    UIImage * tabBarShadow = [UIImage imageNamed:@"clear"];
    
    [[UITabBar appearance] setShadowImage:tabBarBackground];
    [[UITabBar appearance] setBackgroundImage:tabBarShadow];
    UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(0, -9, ScreenWidth, 56)];
    image.image = [UIImage imageNamed:@"BottomNaviBG"];
    [self.tabBar.viewForFirstBaselineLayout addSubview:image];

    
}

- (void)createView {
    self.tabBar.backgroundColor = [UIColor whiteColor];
    
    NSArray * viewControllerNames = @[@"HomePageController", @"HotController", @"FindController", @"MineController"];
    NSArray * normalImageNames = @[@"BottomNaviHome", @"BottomNaviHot", @"BottomNaviDiscover", @"BottomNaviUser"];
    NSArray * selectedImageNames = @[@"BottomNaviHomeActive", @"BottomNaviHotActive", @"BottomNaviDiscoverActive", @"BottomNaviUserActive"];
    
    NSArray * titles = @[@"首页",@"热门",@"发现",@"我的"];
    
    NSMutableDictionary * normalDict = [[NSMutableDictionary alloc] init];
    [normalDict setValue:TextFontWithSize(12.0f) forKey:NSFontAttributeName];
    [normalDict setValue:[UIColor darkTextColor] forKey:NSForegroundColorAttributeName];
    
    NSMutableDictionary * selcetedDict = [[NSMutableDictionary alloc] init];
    [selcetedDict setValue:TextFontWithSize(12.0f) forKey:NSFontAttributeName];
    [selcetedDict setValue:RGBA(0, 122, 223, 1) forKey:NSForegroundColorAttributeName];
    
    _controllers = [[NSMutableArray alloc] init];
    // 创建子视图控制器
    for (int i = 0; i < viewControllerNames.count; i++) {
        Class subClass = NSClassFromString(viewControllerNames[i]);
        ParentViewController * pvc = [[subClass alloc] init];
        pvc.tabBarItem.image = [[UIImage imageNamed:normalImageNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        pvc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        
        [[UITabBarItem appearance] setTitleTextAttributes:normalDict forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:selcetedDict forState:UIControlStateSelected];
        
        RootNavigationController * nvc = [[RootNavigationController alloc] initWithRootViewController:pvc];
        nvc.tabBarItem.title = titles[i];
        [_controllers addObject:nvc];
    }
    
    self.viewControllers = _controllers;
    self.delegate = self;
}

#pragma mark delegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
